//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

//=========================axios前置拦截器==========================//
axios.interceptors.request.use(res=>{
    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;
    }
    return res;
},error => {
    Promise.reject(error)
})
//=========================axios前置拦截器==========================//


//=========================axios后置拦截器==========================//
axios.interceptors.response.use(res=>{
    if(!res.data.success && res.data.message=='noLogin'){
        localStorage.removeItem("token")
        localStorage.removeItem("logininfo")
        location.href= "/login.html"
    }
    return res;
},error => {
    Promise.reject(error)
})
//=========================静态资源拦截器==========================//
var url = location.href

let logininfo = localStorage.getItem("logininfo")
if(url.indexOf("login.html") == -1 && url.indexOf("register.html") == -1
    && url.indexOf("callback.html") == -1 && url.indexOf("binder.html") == -1){
    if (!logininfo){
        location.href = "login.html"
    }
}

//将地址栏？后面的参数装成json对象 - 以后这样的需求很多
function param2JsonObj(url){
    //截取？后面的参数
    var params = url.substring(url.indexOf("?")+1);
    //按照&进行分割 - 获取每一对参数
    var paraArr = params.split("&");//[code=0314GMkl225Gj94lpBnl2BpYxh14GMk6,state=STATE]
    var obj = {};
    for(var i = 0;i<paraArr.length;i++){
        //获取单个参数
        var para = paraArr[i];
        //获取参数名
        var parakey = para.split("=")[0];
        //获取参数值
        var paraValue = para.split("=")[1];
        obj[parakey] = paraValue;
    }
    return obj;
}

